package org.fuckerfucker.todoapp_kotlin.data

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import org.fuckerfucker.todoapp_kotlin.data.model.Todo

// dao must be an interface
@Dao
interface TodoDao {
    @Query("select * from todo_table order by id asc")
    fun getAllData(): LiveData<List<Todo>>
    // suspend means this can be coroutine
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertData(todo: Todo)

    @Update
    suspend fun updateData(todo: Todo)

    @Delete
    suspend fun deleteItem(todo: Todo)

    @Query("delete from todo_table")
    suspend fun deleteAll()

    @Query("select * from todo_table where title like :searchQuery")
    fun searchTable(searchQuery: String): LiveData<List<Todo>>


    // 紧急程度高排序
    @Query("select * from todo_table order by case when priority like 'H%' then 1 when priority like 'M%' then 2 when priority like 'L%' then 3 end")
    fun sortByHighPriority(): LiveData<List<Todo>>
    // 紧急程度低排序
    @Query("select * from todo_table order by case when priority like 'H%' then 3 when priority like 'M%' then 2 when priority like 'L%' then 1 end")
    fun sortByLowPriority(): LiveData<List<Todo>>
}